package ch.drshit.model.picture;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * 
 * @author buehlmanntim / singern
 *
 */
public class Project {
	
	ArrayList<Version> versions = new ArrayList<Version>();
	int activeVersion;
	
	public Project(BufferedImage image) {
		versions.add(new Version(image));
		activeVersion = 0;
	}
	
	public Version getNewVersion() {
		Iterator<Version> i = versions.iterator();
		while (i.hasNext()) {
			Version v = i.next();
			if (versions.indexOf(v) > activeVersion) {
				i.remove();
			}
		}
		versions.add(new Version(versions.get(activeVersion)));
		activeVersion++;
		return versions.get(activeVersion);
	}
	
	public void setActiveVersionIndex(int active) {
		if (!(active < 0) && (active < versions.size())) {
			activeVersion = active;
		}
	}
	
	public Version getActiveVersion() {
		return versions.get(activeVersion);
	}
	
	public void toNext() {
		if (isValidVersion(activeVersion + 1)) {
			activeVersion++;
		}
	}
	
	public void toPrevious() {
		if (isValidVersion(activeVersion - 1)) {
			activeVersion --;
		}
	}
	
	public boolean isValidVersion(int version) {
		if (!(version < 0) && (version < versions.size())) {
			return true;
		}
		return false;
	}
}